<?php

use Biz\model\NewProduct\NewProductWikiModel;
use Biz\service\NewProduct\NewProductService;
use Biz\service\wiki\Wiki;
use Doraemon\model\Member;
use Doraemon\service\User;
use Doraemon\tools\ImgUrl;

/**
 * Description: 往期新品
 * Created by PhpStorm.
 * User: 玉坤
 * Date: 2021/6/16
 * Time: 10:22
 */
class BeforeListAction extends ActionPlugin
{
    public const USER_LEVEL = self::LEVEL_NOTHING;

    public function main()
    {
        //查询符合展示时间的wiki
        $page = $this->getRequest()->getPost('page', 1);
        $per_page = $this->getRequest()->getPost('per_page', 10);
        $time = $this->getRequest()->getPost('time', time());
        //初始化
        $data = [
            'total' => 0,
            'is_over' => 1,
            'current_page' => $page,
            'list' => [],
        ];
        //获取 想要的字段
        $field = [
            'id',
            'brand_name',
            'brand_id',
            'basic_id',
            'price',
            'wiki_id',
            'img',
            'name',
            'desc',
            'features',
            'type',
            'start_time',
            'end_time',
            'question',
            'brand_id',
        ];
        //偏移量，当前页-1乘以每页显示条数
        $start = ($page - 1) * $per_page;
        //获取活动商品列表
        $day = date("Y-m-d 00:00:00", $time);
        $list = NewProductService::getBeforeList($field, $start, $per_page, $day) ?? [];

        $basic_ids = array_column($list, 'basic_id');

        $info = Wiki::getWikiInfoByBasicIds($basic_ids, ['basic_id', 'min_price']);
        $wiki_infos = $info ? array_column($info, null, 'basic_id') : [];

        if (!$list) {
            return $this->setSuccess($data);
        }
        $brand_ids = array_column($list, 'brand_id');
        $brand_list = \Biz\service\brand\Brand::getBrandInfoByIds4Goods($brand_ids);
        $ids = array_column($list, 'id');

        $winner_lists = \Biz\model\NewProduct\NewProductWinnerListModel::select('*', ['nid' => $ids]);
        //处理中奖信息
        $lottery_lists = [];
        if ($winner_lists) {
            $uids = array_column($winner_lists, 'uid');
            $memberList = Member::getUserInfos($uids);
            foreach ($memberList as $k => $item) {
                $aviators = User::getUserAvatarByUid($item['uid'], $item['avatar_version']);
                if (isset($aviators[0]) && $aviators[0]) {
                    $memberList[$k]['icons'] = $aviators[0];
                }
            }
            $user_info = array_column($memberList, null, 'uid');
            foreach ($winner_lists as $k => $winner_list) {
                $lottery_lists[$k]['nick'] = $user_info[$winner_list['uid']]['nick'] ?? '';
                $lottery_lists[$k]['icons'] = $user_info[$winner_list['uid']]['icons'] ?? '';
                $lottery_lists[$k]['nid'] = $winner_list['nid'];
                $lottery_lists[$k]['uid'] = $winner_list['uid'];
                if ($winner_list['type'] == NewProductWikiModel::SUPER_PDT) {
                    $msg = '我抽中了' . $winner_list['prize_name'];
                } else {
                    $msg = '我抽中了50元京东卡';
                }
                $lottery_lists[$k]['msg'] = $msg;
            }
        }
        $lottery_list = \Biz\tools\helper\Common::keyBy($lottery_lists, 'nid');

        //构建列表
        foreach ($list as $k => $item) {
            $list[$k]['brand_info'] = $brand_list[$item['brand_id']] ?? [];
            $list[$k]['lottery_list']['total'] = $lottery_list[$item['id']] ? count($lottery_list[$item['id']]) : 0;
            $list[$k]['lottery_list']['list'] = $lottery_list[$item['id']] ?? [];
            $list[$k]['features'] = explode(',', $item['features']) ?: '';
            $list[$k]['price'] = $wiki_infos[$item['basic_id']]['min_price'] ?? 0;

//            if ($item['type'] == NewProductWikiModel::GRACE_PDT){
//                $list[$k]['price'] = '50.00';
//            }else{
//                $list[$k]['price'] = $item['price'] ? bcdiv($item['price'], 100, 2) : "0.00";
//            }


            $list[$k]['img'] = ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $item["img"],
                ImgUrl::IMAGE_SIZE_SQ_PHONE);
        }
        $current = $page * $per_page;
        $data['total'] = NewProductService::getBeforeListCount($day);
        $data['is_over'] = ($data['total'] <= $current) ? 1 : 0;
        $data['list'] = $list;
        return $this->setSuccess($data);
    }
}